package frontend;

import java.awt.Dimension;
import java.awt.Font;
//import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import concrete.Player;
import concrete.Token;




//VS4E -- DO NOT REMOVE THIS LINE!
public class CreatePlayer extends JDialog {
	
	private String image;
	private int playerNum;
	private Player[] players;
	private Player player = new Player();
	private Token token = new Token();
	private ChooseToken chooseToken;
	private JTextField playerNameInput;
	private JLabel icon;
	private JLabel playerName;
	private JButton createButton;
	private JButton cancelButton;
	private JLabel backgroundImage;
	private static final long serialVersionUID = 1L;
	private static final String PREFERRED_LOOK_AND_FEEL = null;

	public CreatePlayer() {
		initComponents();
	}
	public CreatePlayer(ChooseToken chooseToken, int playerNum, Player[] players, String image) {
		super(chooseToken, true);
		this.image = image;
		this.chooseToken = chooseToken;
		this.playerNum = playerNum;
		this.players = players;
		initComponents();
	}
	
	public Player[] getPlayers() {
		return this.players;
	}
	public void setPlayers(Player[] players) {
		this.players = players;
	}
	private void initComponents() {
		setTitle("Create Player");
		//setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon.gif")));
		setMinimumSize(new Dimension(0, 0));
		setLocationRelativeTo(this.chooseToken);
		setLocation(200,160);
		setResizable(false);
		setLayout(new GroupLayout());
		add(initIcon(), new Constraints(new Leading(17, 10, 10), new Leading(14, 10, 10)));
		add(initPlayerNameInput(), new Constraints(new Leading(120, 133, 6, 6), new Leading(36, 10, 10)));
		add(initPlayerName(), new Constraints(new Leading(124, 10, 10), new Leading(14, 6, 6)));
		add(initCreateButton(), new Constraints(new Leading(188, 6, 6), new Leading(66, 10, 10)));
		add(initCancelButton(), new Constraints(new Leading(122, 10, 10), new Leading(66, 6, 6)));
		add(getBackgroundImage(), new Constraints(new Leading(0, 10, 10), new Leading(2, 10, 10)));
		setSize(269, 140);
	}
	
	private JLabel getBackgroundImage() {
		if (backgroundImage == null) {
			backgroundImage = new JLabel();
			backgroundImage.setIcon(new ImageIcon(getClass().getResource("/images/background/CreatePlayer.jpg")));
			backgroundImage.setMinimumSize(new Dimension(300, 112));
			backgroundImage.setMaximumSize(new Dimension(300, 112));
		}
		return backgroundImage;
	}
	private JButton initCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton1ActionActionPerformed(event);
				}
			});
		}
		return cancelButton;
	}
	private JLabel initPlayerName() {
		if (playerName == null) {
			playerName = new JLabel();
			playerName.setFont(new Font("Dialog", Font.BOLD, 12));
			playerName.setText("Player Name");
		}
		return playerName;
	}
	private JLabel initIcon() {
		if (icon == null) {
			icon = new JLabel();
			icon.setIcon(new ImageIcon(getClass().getResource("/images/tokenChoose/" + image)));
		}
		return icon;
	}
	private JButton initCreateButton() {
		if (createButton == null) {
			createButton = new JButton();
			createButton.setText("Create");
			createButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					jButton0ActionActionPerformed(event);
				}
			});
		}
		return createButton;
	}
	private JTextField initPlayerNameInput() {
		if (playerNameInput == null) {
			playerNameInput = new JTextField();
		}
		return playerNameInput;
	}
	
	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				CreatePlayer dialog = new CreatePlayer();
				dialog.setDefaultCloseOperation(CreatePlayer.DISPOSE_ON_CLOSE);
				dialog.setTitle("create");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}
	public int specifyPosition(){
		int position = 0;
		if(this.image == "02.png")
			position = 1;
		if(this.image == "03.png")
			position = 2;
		if(this.image == "04.png")
			position = 3;
		if(this.image == "05.png")
			position = 4;
		if(this.image == "06.png")
			position = 5;
		if(this.image == "07.png")
			position = 6;
		if(this.image == "08.png")
			position = 7;
		
		return position;	
	}
	
	private void jButton0ActionActionPerformed(ActionEvent event) {
		String name = this.playerNameInput.getText();
		boolean duplicate = false;
		
		for(int i=0; i<this.playerNum; i++) {
			if(name.equals(this.players[i].getName())) {
				duplicate = true;
			}
		}
		
		if(name.isEmpty() || name.length()>5 || name.matches("\\s+") || duplicate) {
			if(name.isEmpty())
			JOptionPane.showMessageDialog(null, "Ooops!!! \nYou have not yet entered a player name. \nPlease provide a player name or cancel to continue.", "Ooops!!!", JOptionPane.PLAIN_MESSAGE);
			
			if(name.matches("\\s+"))
				JOptionPane.showMessageDialog(null, "Ooops!!! \nYour name cannot only consist of white spaces. \nPlease re-enter your name.", "Ooops!!!", JOptionPane.PLAIN_MESSAGE);

			if(name.length()>=5&&!(name.matches("\\s+")))
				JOptionPane.showMessageDialog(null, "Ooops!!! \nName cannot exceed 5 characters. \nPlease re-enter your name.", "Ooops!!!", JOptionPane.PLAIN_MESSAGE);
			
			if(duplicate)
				JOptionPane.showMessageDialog(null, "Sorry \nThis name is already in use. \nPlease enter a different name.", "Ooops!!!", JOptionPane.PLAIN_MESSAGE);
		}
		
		
		else {
			this.player.setName(name);
			this.token.setTokenImage(image);
			this.player.setToken(token);
			this.chooseToken.setPlayer(player, playerNum);
			int position = specifyPosition();
			this.chooseToken.setPlayerPos(position, true);
			this.chooseToken.setPlayerNum(playerNum+1);
			this.chooseToken.buttonDisable(image);
			//this.chooseToken.systemOut();
			this.chooseToken.setVisible(true);
			
			this.dispose();
		}
	}
	private void jButton1ActionActionPerformed(ActionEvent event) {
		if(image=="01.png")
			this.chooseToken.getjButton0().setEnabled(true);
		if(image=="02.png")
			this.chooseToken.getjButton1().setEnabled(true);
		if(image=="03.png")
			this.chooseToken.getjButton2().setEnabled(true);
		if(image=="04.png")
			this.chooseToken.getjButton3().setEnabled(true);
		if(image=="05.png")
			this.chooseToken.getjButton4().setEnabled(true);
		if(image=="06.png")
			this.chooseToken.getjButton5().setEnabled(true);
		if(image=="07.png")
			this.chooseToken.getjButton6().setEnabled(true);
		if(image=="08.png")
			this.chooseToken.getjButton7().setEnabled(true);
		this.chooseToken.setVisible(true);
		//this.chooseToken.systemOut();
		this.dispose();
	}

}

